/**

 */
package com.adkfp.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adkfp.bean.ADKManageUserBean;
import com.adkfp.bean.ADKManageUserRoleBean;
import com.adkfp.bean.MMGridPageVoBean;
import com.adkfp.common.Constant;
import com.adkfp.service.IADKManageUserService;
import com.adkfp.utils.LayuiUtil;

/**
 * class name:SelectclassAction <BR>
 * class description: please write your description <BR>
 * Remark: <BR>
 * @version 1.00 2019年1月24日
 * @author ADK)zhoulin
 */
@Controller
@RequestMapping("aDKManageUser")
public class ADKManageUserAction {
	@Resource(name="aDKManageUserService")
	private IADKManageUserService aDKManageUserService;
	
	/**
 	* Method name: selectAll <BR>
 	* Description: 查询所有记录 <BR>
 	* Remark: <BR>
 	* @param bean
 	* @return  Object<BR>
 	*/
	@RequestMapping("selectAll")
	@ResponseBody
	public Object selectAll(ADKManageUserBean bean) {
		List<ADKManageUserBean> list = new ArrayList<>();
		list = aDKManageUserService.BfindByCondition(bean);
		long count = aDKManageUserService.findByConditionCount(bean);
		return LayuiUtil.data(count, list);
	}
	
	@RequestMapping("selectCll")
	@ResponseBody
	public Object selectCll(ADKManageUserBean bean) {
		bean.setDeleteFlag(0);
		long size = aDKManageUserService.afindByConditionCount(bean);
		//放入分页
				int from = (bean.getPage()-1) * bean.getPageSize();
				bean.setFromNum(from);
				List<ADKManageUserBean> rows = aDKManageUserService.afindByCondition(bean);
				MMGridPageVoBean<ADKManageUserBean> vo = new MMGridPageVoBean<ADKManageUserBean>();
				vo.setTotal(size);
				vo.setRows(rows);
				return vo;
	}
	
	@RequestMapping("selectbll")
	@ResponseBody
	public Object selectbll(ADKManageUserBean bean,Integer id) {
		long size = aDKManageUserService.bfindByConditionCount(bean);
		int from = (bean.getPage()-1) * bean.getPageSize();
		bean.setFromNum(from);
		bean.setId(id);
		List<ADKManageUserBean> rows = aDKManageUserService.bfindByCondition(bean);
		MMGridPageVoBean<ADKManageUserBean> vo = new MMGridPageVoBean<ADKManageUserBean>();
		vo.setTotal(size);
		vo.setRows(rows);
		return vo;
	}

	/**
	 * Method name: index <BR>
	 * Description: 转发到页面 <BR>
	 * Remark: <BR>
	 * @return  String<BR>
	 */
	@RequestMapping("index")
	public String index() {
		return "manage/userList";
	}
	
	/**
	 * Method name: index <BR>
	 * Description: 增加人员 <BR>
	 * Remark: <BR>
	 * @return  String<BR>
	 */
	@RequestMapping("insert")
	@ResponseBody
	public void insert(HttpServletRequest request,ADKManageUserBean bean) {
		if(bean.getId()!=null&&bean.getId()!=0) {
			aDKManageUserService.update(bean);
		}else {
			aDKManageUserService.insert(bean);
		}
	}
	
	//用于修改页面(查找id)
		@RequestMapping("findId")
		public String findId(HttpServletRequest request,String id,ADKManageUserBean bean) {
			System.out.println("xiugai----------"+id);
			if(id != null && !id.equals("")) {
				bean=aDKManageUserService.findById(Integer.parseInt(id));
			}
			request.setAttribute("u",bean);
			return "manage/adduser";
		}
		
		//删除
				@RequestMapping("delete")
				@ResponseBody
				public void delete(String id) {
					System.out.println("MCM----------"+id);
					aDKManageUserService.delete(Integer.parseInt(id));
				}
				
				//增加角色
				@RequestMapping("insertjuese")
				@ResponseBody
				public void insertjuese(int roleId, int userId,ADKManageUserRoleBean bean) {
					bean.setUserId(userId);
					bean.setRoleId(roleId);
					aDKManageUserService.insertjuese(bean);
					}
				
				/**
				 * 修改密码
				 */
				@RequestMapping(value="updatePassword")
				@ResponseBody
				public String updateManageUser(HttpServletRequest request, String password){
					try {
						HttpSession session = request.getSession();
						Integer id = (Integer) session.getAttribute(Constant.SESSION_USERID_LONG);
						aDKManageUserService.updateManageUserPassword(id, password);
						return "SUCCESS";
					} catch (Exception e) {
						return "ERROR";
					}
				}
	
				/**
				 * 判断密码是否正确
				 */
				@RequestMapping(value="checkUserPassword")
				@ResponseBody
				public boolean checkUserPassword(HttpServletRequest request, String password){
					//组织参数
					HttpSession session = request.getSession();
					Integer id = (Integer) session.getAttribute(Constant.SESSION_USERID_LONG);
					ADKManageUserBean userBean = aDKManageUserService.findById(id);
					if(userBean.getPassword().equalsIgnoreCase(password)){
						return true;
					}
					return false;
				}
				
}
